/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.EhCacheUtils;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.JsonResponse;
import com.thinkgem.jeesite.common.utils.Preview;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.pro.entity.Project;
import com.thinkgem.jeesite.modules.pro.entity.ProjectTask;
import com.thinkgem.jeesite.modules.pro.entity.TaskGroup;
import com.thinkgem.jeesite.modules.pro.service.ProjectTaskService;
import com.thinkgem.jeesite.modules.pro.service.TaskGroupService;

/**
 * 任务组管理Controller
 * @author fanqinglong
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/taskGroup")
public class TaskGroupController extends BaseController {
	@Autowired
	private ProjectTaskService projectTaskService;
	@Autowired
	private TaskGroupService taskGroupService;
	@ModelAttribute
	public TaskGroup get(@RequestParam(required=false) String id) {
		TaskGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = taskGroupService.get(id);
		}
		if (entity == null){
			entity = new TaskGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:taskGroup:view")
	@RequestMapping(value = {"index"})
	public String index(){
		return "modules/pro/groupIndex";
	}
	
	@RequiresPermissions("pro:taskGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(TaskGroup taskGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		taskGroup.setDelFlag(StringUtils.isEmpty(taskGroup.getDelFlag())?"0":taskGroup.getDelFlag());
		Page<TaskGroup> page = taskGroupService.findPage(new Page<TaskGroup>(request, response), taskGroup); 
		model.addAttribute("page", page);
		model.addAttribute("e",taskGroup);
		return "modules/pro/taskGroupList";
	}

	@RequiresPermissions("pro:taskGroup:view")
	@RequestMapping(value = "form")
	public String form(TaskGroup taskGroup, Model model) {
		model.addAttribute("taskGroup", taskGroup);
		return "modules/pro/taskGroupForm";
	}

	@RequiresPermissions("pro:taskGroup:edit")
	@RequestMapping(value = "save")
	public String save(TaskGroup taskGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, taskGroup)){
			return form(taskGroup, model);
		}
		taskGroupService.save(taskGroup);
		addMessage(redirectAttributes, "保存任务组成功");
		return "redirect:"+Global.getAdminPath()+"/pro/taskGroup/?repage&project.id="+taskGroup.getProject().getId();
	}
	
	@RequiresPermissions("pro:taskGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(TaskGroup taskGroup, RedirectAttributes redirectAttributes) {
		Integer count = projectTaskService.findByGroupIdAndStatus(taskGroup.getId());
		if (count > 0) {
			addMessage(redirectAttributes, "有未完成任务，不能归档");
			return "redirect:"+Global.getAdminPath()+"/pro/taskGroup/?project.id="+taskGroup.getProject().getId()+"&repage";
		}
		taskGroupService.delete(taskGroup);
		addMessage(redirectAttributes, "归档任务组成功");
		return "redirect:"+Global.getAdminPath()+"/pro/taskGroup/?project.id="+taskGroup.getProject().getId()+"&repage";
	}

	@RequiresPermissions("pro:taskGroup:edit")
	@RequestMapping(value = "getByName")
	@ResponseBody
	public String getByName(HttpServletRequest request,TaskGroup taskGroup) {
		String result = "false";
		String projectId = request.getParameter("projectId");
		taskGroup.setProject(new Project(projectId));
		TaskGroup resultGroup = taskGroupService.getByName(taskGroup);
		if (null == resultGroup || resultGroup.getId().equals(taskGroup.getId())) {
			result = "true";
		}
		return result;
	}
	
	@RequiresPermissions("pro:taskGroup:import")
	@RequestMapping(value = "previewList")
	public String previewList(TaskGroup taskGroup,Model model){
		model.addAttribute("e", taskGroup);
		return "modules/pro/importList";
	}
	
	@RequiresPermissions("pro:taskGroup:import")
	@RequestMapping(value = "preview")
	public String preview(HttpServletRequest request,MultipartFile file,TaskGroup taskGroup,Model model){
		Preview<ProjectTask> preview = projectTaskService.getPreview(request,file, taskGroup.getProject().getId());
		//将预览结果返回到页面去显示出来
		model.addAttribute("preview", preview);
		model.addAttribute("e", taskGroup);
		return "modules/pro/importList";
	}
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions("pro:taskGroup:import")
	@RequestMapping(value = "importExcel")
	@ResponseBody
	public JsonResponse<ProjectTask> importExcel(HttpServletRequest request){
		String key = request.getParameter("key");
		JsonResponse<ProjectTask> jsonResponse = new JsonResponse<>("200", "导入成功");
		List<ProjectTask> list = null;
		Object value = EhCacheUtils.get("temp", key);
		if (null != value) {
			list = (List<ProjectTask>) value;
		}
		if (null == list) {
			jsonResponse.setStatus("500");
			jsonResponse.setMessage("获取数据失败");
			return jsonResponse;
		}
		//批量导入
		try {
			projectTaskService.importBatch(list);
		} catch (Exception e) {
			jsonResponse.setStatus("500");
			jsonResponse.setMessage(e.getMessage());
			return jsonResponse;
		}
		return jsonResponse;
	}
	
	@RequiresPermissions("pro:taskGroup:import")
	@RequestMapping(value = "down")
	@ResponseBody
	public String down(HttpServletRequest request,HttpServletResponse response){
		String templateUrl = request.getSession().getServletContext().getRealPath("/")+"upload/import.xlsx";
		File file = new File(templateUrl);
		return FileUtils.downFile(file, request, response);
	}
}